// pages/center/center.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowFrom1: false,
    userMsg:'',
    UserId: '', //用户id
  },
  onLoad:function(){
    //登陆状态判断
    let that = this;
    wx.getStorage({
      key: 'token',
      success(res) {
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          url: 'http://47.104.142.216/TP/public/index.php/index/User/VerificationToken', //本地设置不校验合法域名
          data: {
            token: res.data
          },
          method: 'POST',
          dataType: 'JSON',
          success: function (res) {
            let keys = JSON.parse(res.data)
            if (keys.errorCode == 10009) {
              wx.showToast({
                title: '您还未登录，请先登录',
                icon: 'none',
                duration: 1500
              })
              //TODO 跳转
              wx.redirectTo({
                url: '../Login/Login',
              })
            } else if (keys.errorCode == 10017) {
              wx.removeStorage({
                key: 'token',
                success(res) {
                  // console.log(res)
                }
              })
              wx.showToast({
                title: '您还未登录，请先登录',
                icon: 'none',
                duration: 1500
              })
              //TODO 跳转
              wx.redirectTo({
                url: '../Login/Login',
              })
            } else {
              //! 发送请求获取数据
              that.setData({
                UserId: keys.data
              })
              var userid = that.data.UserId;
              var newuserid = userid.user_id
              wx.setStorageSync('OoOoUserid', newuserid);
            }
          }
        })
      },
      fail: function () {
        wx.showToast({
          title: '您还未登录，请先登录',
          icon: 'none',
          duration: 1500
        })
        //TODO 跳转
        wx.redirectTo({
          url: '../Login/Login',
        })
      }
    });
    /*---------------------------------------------*/
},
  //个人资料
  wxMsg:function(){
    wx.navigateTo({
      url: '/pages/centerMsg/centerMsg'
    })
  },
  //安全设置
  wxSafety:function(){
    wx.navigateTo({
      url: '/pages/centerSafety/centerSafety'
    })
  },
  //收货地址
  wxAdd:function(){
    wx.navigateTo({
      url: '/pages/centerAdd/centerAdd'
    })
  },
  //我的收藏
  wxCollect:function(){
    wx.navigateTo({
      url: '/pages/centerCollect/centerCollect'
    })
  },
  //我的订单
  wxOrder:function(){
    wx.navigateTo({
      url: '/pages/centerOrder/centerOrder'
    })
  },
   //我的钱包
   wxWallet:function(){
    wx.navigateTo({
      url: '/pages/centerWallet/centerWallet'
    })
  },
  //注销
  loginoutt:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要注销吗？',
      success (res) {
        if (res.confirm) {
          wx.showToast({
            title: '已注销！',
            icon: 'success',
            duration: 3000
          })
          wx.removeStorage({
            key: 'token',
            success: function(res) {
              that.onLoad()
              return;
            },
          })
      
          
        } else if (res.cancel) {

        }
      }
    })
  }
  
})